package org.example.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

//@Configuration
public class MybatisConfig {
    /*数据源
    别名
    对应的Mapper   */



    @Bean                //这个会找到jdbcConfig中的返回的配置文件数据(无论谁先复制)
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean ssfBean = new SqlSessionFactoryBean();
        ssfBean.setTypeAliasesPackage("org.example.entity");
        ssfBean.setDataSource(dataSource);

        ssfBean.setMapperLocations
                (new PathMatchingResourcePatternResolver()
                    .getResources("classpath:mapper/*.xml"));

        return ssfBean;
    }

    //配置mapper的注册问题
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("org.example.mapper");
        return msc;
    }

}
